local ls = require("luasnip")
local s = ls.snippet
local sn = ls.snippet_node
local t = ls.text_node
local i = ls.insert_node
local f = ls.function_node
local d = ls.dynamic_node
local fmt = require("luasnip.extras.fmt").fmt
local fmta = require("luasnip.extras.fmt").fmta
local rep = require("luasnip.extras").rep

-- 加载所有语言的代码片段
local snippets_folder = vim.fn.stdpath("config") .. "/lua/plugins/snippets/languages/"

-- 自动加载代码片段文件夹中的所有代码片段
local function load_snippets()
  -- 确保文件夹存在
  vim.fn.mkdir(snippets_folder, "p")

  -- 遍历文件夹中的所有 .lua 文件
  local snippets_files = vim.fn.glob(snippets_folder .. "*.lua", false, true)
  for _, file in ipairs(snippets_files) do
    local snippets = dofile(file)
    if snippets then
      -- 如果文件返回了片段表，添加到 all 文件类型
      ls.add_snippets("all", snippets)
    end
  end
end

-- 设置代码片段配置
ls.config.set_config({
  history = true,
  store_selection_keys = "<Tab>",
  updateevents = "TextChanged,TextChangedI",
  enable_autosnippets = true,
  ext_opts = {
    [require("luasnip.util.types").choiceNode] = {
      active = {
        virt_text = { { "●", "GruvboxOrange" } },
      },
    },
  },
  -- 使用文件的缩进设置
  -- indent = {
  --   use_treesitter = true,  -- 使用 treesitter 来检测缩进
  --   follow_current_line = true,  -- 跟随当前行的缩进
  -- },
})

-- 加载代码片段
load_snippets()

-- 导出模块
return {
  ls = ls,
  s = s,
  sn = sn,
  t = t,
  i = i,
  f = f,
  d = d,
  fmt = fmt,
  fmta = fmta,
  rep = rep,
  load_snippets = load_snippets,
}
